//可以选择用单链表实现，也可以用数组实现
//这里选择用数组实现
typedef struct 
{
    int* arr;
    int hand;
    int tail;
    int k;
}MyCircularQueue;
//函数声明
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
//多开辟一个空间
MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    obj->arr = (int*)malloc((k + 1) * sizeof(int)); 
    if(obj->arr == NULL)
    {
        perror("malloc fail!");
        exit(1);
    }
    obj->hand = 0;
    obj->tail = 0;
    obj->k = k;
    return obj;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    if(myCircularQueueIsFull(obj))
    {
        return false;
    }
    obj->arr[obj->tail] = value;
    obj->tail++;
    obj->tail = (obj->tail) % (obj->k + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    if(myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    obj->hand++;
    obj->hand = (obj->hand) % (obj->k + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    if(myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->arr[obj->hand];
}

int myCircularQueueRear(MyCircularQueue* obj)
{
    if(myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->arr[(obj->tail - 1 + obj->k + 1) % (obj->k + 1)];  
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    return obj->hand == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    return (obj->tail + 1) % (obj->k + 1) == obj->hand;    
}

void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->arr);
    free(obj);    
}